package com.ruoyi.cmdb.controller;

import com.jcraft.jsch.JSchException;
import com.ruoyi.cmdb.util.*;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Path;


/**
 * 测试scp
 *
 */
@RestController
@RequestMapping("/cmdb/testScp")
@Api(tags = "5.testScp")
public class TestScpController {
    @Value("${ssh.privateKey}") //私钥字符串
    private String privateKeyString;

    @Value("${randomLength}") //随机字符串长度
    private Integer randomLength;

    /**
     * testScpByPrivateKeySend
     */
    @GetMapping("/testScpByPrivateKeySend")
    @ApiOperation("testScpByPrivateKeySend")
    public AjaxResult testScpByPrivateKeySend()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        String user = "root";
        int port = 22; // 默认SSH端口是22

        Path privateKeyPath=SavePrivateKeyToFile.save(privateKeyString,randomLength);
        String privateKeyAbsolutePath=String.valueOf(privateKeyPath.toAbsolutePath());
        String localFilePath = privateKeyAbsolutePath;
        String remoteFilePath="/tmp/file.txt1";

        SshScpByPrivateKey.send(host,port,user,privateKeyAbsolutePath,localFilePath,remoteFilePath);

        DeleteLocalFile.delete(privateKeyPath);
        String rps="ssh testScp send success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testScpByPrivateKeyGet
     */
    @GetMapping("/testScpByPrivateKeyGet")
    @ApiOperation("testScpByPrivateKeyGet")
    public AjaxResult testScpByPrivateKeyGet()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        String user = "root";
        int port = 22; // 默认SSH端口是22

        Path privateKeyPath=SavePrivateKeyToFile.save(privateKeyString,randomLength);
        String privateKeyAbsolutePath=String.valueOf(privateKeyPath.toAbsolutePath());
        String remoteFilePath="file.txt1";
        String localFilePath = "file.txt1";

        SshScpByPrivateKey.get(host,port,user,privateKeyAbsolutePath,remoteFilePath,localFilePath);

        DeleteLocalFile.delete(privateKeyPath);
        String rps="ssh testScp get success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testScpByPasswordSend
     */
    @GetMapping("/testScpByPasswordSend")
    @ApiOperation("testScpByPasswordSend")
    public AjaxResult testScpByPasswordSend()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        int port = 22; // 默认SSH端口是22
        String user = "root";
        String password="123`abcA";

        //Path privateKeyPath=SavePrivateKeyToFile.save(privateKeyString,randomLength);
        //String privateKeyAbsolutePath=String.valueOf(privateKeyPath.toAbsolutePath());
        String localFilePath = "file.txt1";
        String remoteFilePath="/tmp/file.txt1";

        SshScpByPassword.send(host,port,user,password,localFilePath,remoteFilePath);

        String rps="ssh testScp send success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testScpByPasswordGet
     */
    @GetMapping("/testScpByPasswordGet")
    @ApiOperation("testScpByPasswordGet")
    public AjaxResult testScpByPasswordGet()  throws JSchException, InterruptedException  {
        String host = "192.168.32.144";
        int port = 22; // 默认SSH端口是22
        String user = "root";
        String password="123`abcA";

        String remoteFilePath="file.txt1";
        String localFilePath = "file.txt1";

        SshScpByPassword.get(host,port,user,password,remoteFilePath,localFilePath);

        String rps="ssh testScp get success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
    
}
